﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;

namespace ManageTicket.Ticket
{
    public partial class frmFindTicket : Form
    {
        #region Contructor
        public frmFindTicket()
        {
            InitializeComponent();
        }
        #endregion

        #region Variable
        clsMethod4All objMethod = new clsMethod4All();
        ManageTicket.Library.Common.CommonFunction clsCommonFunc = new ManageTicket.Library.Common.CommonFunction();
        public ArrayList arrResult; 
        #endregion

        #region Event
        private void frmFindTicket_Load(object sender, EventArgs e)
        {
            arrResult = new ArrayList();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            Business.Voucher boVoucher = new Business.Voucher();
            Business.Employee boEmployee = new Business.Employee();
            Business.Customer boCustomer = new Business.Customer();
            Business.Provider boProvider = new Business.Provider();

            object[] oParam = new object[14];
            oParam[0] = txtVoucherNo.Text;
            oParam[1] = txtEmployeeId.Text;
            oParam[2] = txtFullName.Text;
            oParam[3] = txtProviderId.Text;
            oParam[4] = txtProviderName.Text;
            oParam[5] = txtCustomerId.Text;
            oParam[6] = txtCustomerName.Text;
            DateTime dFrom;
            if (dtpDateFrom.Checked)
                dFrom = new DateTime(dtpDateFrom.Value.Year, dtpDateFrom.Value.Month, dtpDateFrom.Value.Day, 0, 0, 0);
            else
                dFrom = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0);
            oParam[7] = dFrom;
            DateTime dTo;
            if (dtpDateTo.Checked)
                dTo = new DateTime(dtpDateTo.Value.Year, dtpDateTo.Value.Month, dtpDateTo.Value.Day, 23, 59, 59);
            else
                dTo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, objMethod.GetEndDayOfMonth(DateTime.Now.Month, DateTime.Now.Year), 23, 59, 59);
            oParam[8] = dTo;
            oParam[9] = txtSumBuyVndFrom.Text != "" ? double.Parse(txtSumBuyVndFrom.Text) : 0;
            oParam[10] = txtSumBuyVndTo.Text != "" ? double.Parse(txtSumBuyVndTo.Text) : 0;
            oParam[11] = txtSumSaleVndFrom.Text != "" ? double.Parse(txtSumSaleVndFrom.Text) : 0;
            oParam[12] = txtSumSaleVndTo.Text != "" ? double.Parse(txtSumSaleVndTo.Text) : 0;
            if (cboStatus.Text == "Hiện tại")
                oParam[13] = "CURRENT";
            else if (cboStatus.Text == "Kết thúc")
                oParam[13] = "FINISH";
            else
                oParam[13] = "";
            

            //goi ham search
            ArrayList arr = new ArrayList();
            arr = boVoucher.SearchVoucher(oParam);

            //danh sach tra ve
            lvwVoucher.Items.Clear();
            ListViewItem item;
            DataObjects.Employee oEmp;
            DataObjects.Customer oCus;
            DataObjects.Provider oPro;
            foreach (DataObjects.Voucher oVou in arr)
            {
                item = new ListViewItem();
                item.Text = oVou.VoucherNo;
                item.SubItems.Add(oVou.DateOfVoucher.Value.ToString("dd/MM/yyyy hh:mm tt"));
                
                item.SubItems.Add(oVou.CustomerId);
                oCus = boCustomer.GetObjectByPK(oVou.CustomerId);
                if (oCus != null)
                    item.SubItems.Add(oCus.CustomerName);
                else
                    item.SubItems.Add("");

                item.SubItems.Add(oVou.ProviderId);
                oPro = boProvider.GetObjectByPK(oVou.ProviderId);
                if (oPro != null)
                    item.SubItems.Add(oPro.ProviderName);
                else
                    item.SubItems.Add("");

                item.SubItems.Add(oVou.EmployeeId);
                oEmp = boEmployee.GetObjectByPK(oVou.EmployeeId);
                if (oEmp != null)
                    item.SubItems.Add(oEmp.LastName + " " + oEmp.FirstName);
                else
                    item.SubItems.Add("");

                item.SubItems.Add(objMethod.FormatCurency(oVou.BuySumVnd.ToString()));
                item.SubItems.Add(objMethod.FormatCurency(oVou.SaleSumVnd.ToString()));

                lvwVoucher.Items.Add(item);
            }
            lblCount.Text = "Tổng: " + lvwVoucher.Items.Count.ToString();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            arrResult = new ArrayList();
            DataObjects.Voucher oVou;
            foreach (ListViewItem item in lvwVoucher.CheckedItems)
            {
                oVou = new DataObjects.Voucher();
                oVou.VoucherNo = item.Text;

                arrResult.Add(oVou);
                clsCommonFunc.AddListVoucher(oVou);
            }
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            arrResult = new ArrayList();
            this.Close();
        }

        private void txtSumBuyVndFrom_KeyPress(object sender, KeyPressEventArgs e)
        {
            objMethod.InputNumber(e, txtSumBuyVndFrom.Text);
        }

        private void txtSumBuyVndTo_KeyPress(object sender, KeyPressEventArgs e)
        {
            objMethod.InputNumber(e, txtSumBuyVndTo.Text);
        }

        private void txtSumSaleVndFrom_KeyPress(object sender, KeyPressEventArgs e)
        {
            objMethod.InputNumber(e, txtSumSaleVndFrom.Text);
        }

        private void txtSumSaleVndTo_KeyPress(object sender, KeyPressEventArgs e)
        {
            objMethod.InputNumber(e, txtSumSaleVndTo.Text);
        }

        private void txtSumBuyVndFrom_Leave(object sender, EventArgs e)
        {
            txtSumBuyVndFrom.Text = objMethod.FormatCurency(txtSumBuyVndFrom.Text);
        }

        private void txtSumBuyVndTo_Leave(object sender, EventArgs e)
        {
            txtSumBuyVndTo.Text = objMethod.FormatCurency(txtSumBuyVndTo.Text);
        }

        private void txtSumSaleVndFrom_Leave(object sender, EventArgs e)
        {
            txtSumSaleVndFrom.Text = objMethod.FormatCurency(txtSumSaleVndFrom.Text);
        }

        private void txtSumSaleVndTo_Leave(object sender, EventArgs e)
        {
            txtSumSaleVndTo.Text = objMethod.FormatCurency(txtSumSaleVndTo.Text);
        }

        private void chkAll_CheckedChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem item in lvwVoucher.Items)
            {
                item.Checked = chkAll.Checked;
            }
        }
        #endregion
        
    }
}
